package com.weixing.mall.provider.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.api.model.vo.FrontGoodsVo;
import com.weixing.mall.provider.mapper.GoodsMapper;
import com.weixing.mall.provider.mapper.GoodsVisitMapper;
import com.weixing.mall.provider.model.domain.GoodsConcern;
import com.weixing.mall.provider.model.domain.GoodsVisit;
import com.weixing.mall.provider.model.dto.GoodsVisitDto;
import com.weixing.mall.provider.model.query.FrontGoodsQuery;
import com.weixing.mall.provider.model.vo.GoodsVisitVo;
import com.weixing.mall.provider.model.vo.GoodsVo;
import com.weixing.mall.provider.service.IGoodsVisitService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author lgc
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2019/12/10 15:04
 */
@Service
public class GoodsVisitServiceImpl extends ServiceImpl<GoodsVisitMapper,GoodsVisit> implements IGoodsVisitService {

    @Autowired
    private GoodsVisitMapper goodsVisitMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<GoodsVisitVo> selectByMemberId(Long member_id) {
        return goodsVisitMapper.selectByMemberId(member_id);
    }

    @Override
    public List<Map<String,Object>> selectByMemberIdPage( Page page,Long member_id) {
        IPage<GoodsVisitVo> tt=goodsVisitMapper.selectByMemberIdPage(page,member_id);
        return handerData(tt.getRecords());
    }
    private  List<Map<String,Object>>  handerData(List<GoodsVisitVo> goodsVoList){
                List<Map<String,Object>> result=new ArrayList<Map<String,Object>>();
                List<String > keyList=Lists.newArrayList();
                for(GoodsVisitVo vo:goodsVoList){
                    DateTime time=new DateTime();
                    time.setTime(vo.getCreatedTime().getTime());
            keyList.add( time.toString("yyyy-MM-dd"));
        }
        //key去重複
        HashSet h = new HashSet(keyList);
        keyList.clear();
        keyList.addAll(h);
        for(String key:keyList){
            Map<String,Object> tmp=new HashMap<String,Object>() ;
            List<GoodsVisitVo> voList= Lists.newArrayList();
            for(int i=0;i<goodsVoList.size();i++){
                GoodsVisitVo goodsVo=goodsVoList.get(i);
                DateTime time=new DateTime();
                time.setTime(goodsVo.getCreatedTime().getTime());
                String tt=time.toString("yyyy-MM-dd");
                if(tt.equals(key)){
                    voList.add(goodsVo);
                    goodsVoList.remove(i);
                    i--;
                }
            }
            tmp.put("vo",voList);
            tmp.put("time",key);
            result.add(tmp);
        }
        return result;

    }
    @Override
    public int selectMyHistory(Long memberId) {
        return goodsVisitMapper.selectMyHistory(memberId);
    }

    @Override
    public void deleteByMemberId(Long memberId) {
         goodsVisitMapper.deleteByMemberId(memberId);

    }

    @Override
    public IPage<FrontGoodsVo> save(GoodsVisitDto goodsVisitDto, Page page) {
        GoodsVisit goodsVisit = new GoodsVisit();
        BeanUtils.copyProperties(goodsVisitDto,goodsVisit);
        save(goodsVisit);
        FrontGoodsQuery frontGoodsQuery = new FrontGoodsQuery();
        frontGoodsQuery.setInformation(goodsVisitDto.getInformation());
        IPage<FrontGoodsVo> list = goodsMapper.findBycatAndPrice(page, frontGoodsQuery);
        return list;
    }
}
